<template>
	<view class="main">
		<u-image width="100%" height="100%" :src="src" class="img"></u-image>

		<view class="cont">
			<view class="icon-1">
				邀请函
			</view>

			<view v-if="pushall" style="display: contents;">
				<u-image width="183rpx" height="183rpx"
					src="https://yh-sys.oss-cn-beijing.aliyuncs.com/44298AE0940F479A8FFAA6C43A89A13A%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20211214113859.png?Expires=4793053181&OSSAccessKeyId=LTAI4GL8SwAiLh3sSPXDS7z9&Signature=helq%2BONDaWr9qSYCm%2Fs2TelETeo%3D"
					shape="circle"></u-image>
				<view class="icon-2"> 欢迎加入 </view>
				<view class="icon-3">【五育通劳动教育云】 </view>
			</view>
			<view v-else style="display: contents;">
				<u-image width="183rpx" height="183rpx" :src="icon" shape="circle"></u-image>
				<view class="icon-2"> {{trname}}老师邀请您加入 </view>
				<view class="icon-3"> 【{{bjmc==''?schoolname:bjmc}}】 </view>
			</view>

			<view class="icon-4">
				快来加入与你的小伙伴们一起学习吧！
			</view>
			<u-icon name="arrow-down-fill" color="#ffa914"></u-icon>
			<u-button type="primary" shape="circle" :custom-style="customStyle" @click="chkuser" :ripple="true"
				ripple-bg-color="#d7dbf8">加入</u-button>
		</view>
		<u-top-tips ref="uTips"></u-top-tips>
		<u-toast ref="uToast" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pushall: false,
				schoolid: '',
				userid: '',
				trname: '',
				schoolname: '',
				// classid: '',
				icon: '',
				src: 'https://yhykt.oss-cn-beijing.aliyuncs.com/37525DEA213049908AA4D16A3613F002join.png?Expires=4788905369&OSSAccessKeyId=LTAI4Fgsq7J3XQi7e2SRfzjJ&Signature=TtNI5i6zgV%2B8uUpWjifPC9yVElg%3D',
				customStyle: {
					width: '436rpx',
					height: '92rpx',
					marginTop: '45rpx'
				},
				show: 0,
				userInfo: {},
				code: '',
				unionid: '',
				openid: '',
				user: {},
				form:uni.getStorageSync('accont'),
				bjmc: '',
				stuicon: 'https://yhykt.oss-cn-beijing.aliyuncs.com/AF87304F4B8146FCBBE2799A1E7DF309%E7%94%A8%E6%88%B7.jpg?Expires=4763169971&OSSAccessKeyId=LTAI4Fgsq7J3XQi7e2SRfzjJ&Signature=w7QHRNx9EyQqEuDRPv18P2Nayqg%3D',
				accont:uni.getStorageSync('accont')
			}
		},
		onLoad: function(options) {
			this.schoolid = options.schoolid;
			this.schoolname = options.schoolname;
			// this.userid=options.userid;
			this.trname = options.trname;
			this.icon = options.icon;
			this.bjmc = options.bjmc;
			console.log(options);
			if (options.schoolid == undefined) {
				this.pushall = true
			}
		},
		methods: {
			chkuser() {
				
				//登录成功后，存储账号密码
				console.log(uni.getStorageSync('accont'));
				console.log(!!this.accont);
				if(!!this.accont){
					
					if (this.accont.pwd != '') {
						this.pwdLogin()
					} else {
						this.codeLogin()
					}
				}else{
					wx.login({
						success: res => {
							var _this = this;
							_this.$api.post({
								url: '/comm/getAppletUnionid'
							}, {
								code: res.code
							}).then(s => {
								console.log(s);
								_this.unionid = s.unionid;
								uni.setStorageSync('unionid', s.unionid);
								if (s.code == 0) { //有用户  ：选择角色进入
									uni.setStorageSync('user', s.user);
									uni.setStorageSync('roles', s.roles);
									uni.setStorageSync('role', s.roles[0]);
									this.$refs.uToast.show({
										title: '检测到您已是注册用户！2秒后自动跳转~',
										type: 'warning',
										duration: '2300'
									})
									setTimeout(() => {
										_this.tohome(s.roles);	
										
									}, 2300);
								} else { //无用户 ：进入注册申请
									uni.navigateTo({
										url: `/team/register/selectrole`
									});
								}
							})
						}
					})
				}
				
			},
			pwdLogin() {
				this.$api.post({
					url: '/sysuser/login'
				}, this.accont).then(res => {
					if (res.code == "0") {
			
						//登录成功后，存储账号密码
						uni.setStorageSync('accont', this.accont)
						console.log(uni.getStorageSync('accont'));
						switch (res.result) {
							case 'password_error':
								this.$refs.uToast.show({
									title: '密码错误',
									type: 'warning',
								});
								break;
							case 'none':
								this.$refs.uToast.show({
									title: '无此用户，请先注册',
									type: 'warning',
								});
								break;
							case 'overtime':
								this.$refs.uToast.show({
									title: '服务到期，请联系学校管理员',
									type: 'warning',
								});
								break;
							default:
								uni.setStorageSync("user", res.user);
								uni.setStorageSync("roles", res.roles);
								console.log(res.roles)
								uni.setStorageSync('role', res.roles[0]);
								// console.log(">>>>>unionid    " + this.unionid)
								// console.log(res.user)
								if (res.user.unionid == '') {
									this.updateUser(res.user.id, res.roles); //更新用户unionid信息
								} else {
									this.tohome(res.roles);
								}
								break;
						}
					}
				})
			},
			codeLogin() {
				let params = {
					mobile: this.accont.usercode,
					pwd: ''
				};
				this.$api.post({
					url: '/sysuser/mobilelogin'
				}, params).then(res => {
					if (res.code == "0") {
						//登录成功后，存储账号密码
						uni.setStorageSync('accont', params)
						console.log(uni.getStorageSync('accont'));
						switch (res.result) {
							case 'password_error':
								this.$refs.uToast.show({
									title: '密码错误',
									type: 'warning',
								});
								break;
							case 'none':
								this.$refs.uToast.show({
									title: '无此用户，请先注册',
									type: 'warning',
									url: '/team/register/selectrole'
								});
								break;
							case 'overtime':
								this.$refs.uToast.show({
									title: '服务到期，请联系学校管理员',
									type: 'warning',
								});
								break;
							default:
								uni.setStorageSync("user", res.user);
								uni.setStorageSync("roles", res.roles);
								console.log(res.roles)
								uni.setStorageSync('role', res.roles[0]);
								console.log(">>>>>unionid    " + this.unionid)
								console.log(res.user)
								if (res.user.unionid == '') {
									this.updateUser(res.user.id, res.roles); //更新用户unionid信息
								} else {
									this.tohome(res.roles);
								}
								break;
						}
					}
				})
			},
			tohome(roles) {
				console.log('自动登录');
				if (roles.length == 1) {
					if (roles[0] == 'tr') {
						uni.reLaunch({
							url: '/pages/home/home'
						})
					} else if (roles[0] == 'stu') {
						uni.reLaunch({
							url: '/pageHome/stuhome/home'
						})
					} else if (roles[0] == 'team') {
						uni.reLaunch({
							url: '/team/kc/changeSchool'
						})
					} else if (roles[0] == 'admin') {
						let ur = uni.getStorageSync("user");
						this.$api.post({
							url: '/yschool/getById'
						}, {
							id: ur.schoolId
						}).then(res => {
							console.log(res);
							uni.setStorageSync("currSchool", res.school)
							uni.reLaunch({
								url: '/team/schoolHome'
							})
						})
					}
				} else if (roles.length > 1) {
					uni.reLaunch({
						url: '/pages/index/selectRole'
					})
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	.main {
		width: 100%;
		height: 100%;
		overflow: hidden;

		.img {
			width: 100%;
			height: 100%;
			position: absolute;
			top: 0;
			left: 0;
			z-index: -1;
		}

		.cont {
			box-sizing: border-box;
			margin: 30% auto 5%;
			width: 94%;
			height: 80%;
			display: flex;
			flex-direction: column;
			align-items: center;
			align-content: space-around;
			justify-content: center;
			color: #000;
			z-index: 1000;
			background-color: #fff;
			border-radius: 56rpx;

			.icon-1 {
				font-size: 46rpx;
				color: #3d82f1;
				margin-bottom: 131rpx;
			}

			.icon-2 {
				margin: 59rpx 0 30rpx;
				font-size: 36rpx;
				color: #2e323d;
			}

			.icon-3 {
				font-size: 36rpx;
				color: #4085f9;
			}

			.icon-4 {
				margin: 27rpx 0;
				font-size: 30rpx;
				color: #ffa914;
			}

			button {
				width: 436rpx;
				height: 92rpx;
			}
		}
	}
</style>
